package com.maxjay.system.web.controller.struts;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.maxjay.system.bo.UserInfo;
import com.maxjay.system.constant.YesNoConstant;
import com.maxjay.system.po.TSysUser;
import com.maxjay.system.service.UserService;

@Scope("prototype")
@Component("registerController")
public class RegisterController extends BaseController {
	
	private static Logger logger = Logger.getLogger(RegisterController.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private UserService userService;
	
	private TSysUser user;
	
	public String execute(){
		if(userService.validateUserExist(user.getLoginId())){
			this.addFieldError("user.loginId", "该登录名已经存在，请使用其他名字登录");
			return "register";
		}
		user.setIsManager(YesNoConstant.NO);
		userService.save(user);
		request.getSession().setAttribute(UserInfo.SYS_USERINFO, UserInfo.initFrom(user));
		return SUCCESS;
	}
	
	public String validateLoginIdUserful(){
		String loginId = request.getParameter("loginId");
		boolean exist = userService.validateUserExist(loginId);
		logger.debug("验证登录ID[" + loginId + "]是否存在，结果为" + exist);
		if(!exist){
			outputJsonStream("true");
		} 
		else {
			Map<String, String> map = new HashMap<String, String>();
			map.put(loginId, "该登录ID已存在");
			outputJsonStream(map);
		}
		return null;
	}

	public TSysUser getUser() {
		return user;
	}

	public void setUser(TSysUser user) {
		this.user = user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
